﻿/*******************************************************************************
#                    DomainBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-8-18 20:07:18
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
#endregion

namespace ExpertMIS.Brokers
{
    public class DomainBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateDomainEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateDomainEntity", entity);
        }

        private object InsertDomainEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertDomainEntity", entity);
        }

        private object DeleteDomainEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteDomainEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectDomainEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                if (entity.EntityState == EntityState.New)
                {
                    InsertDomainEntity(entity);
                    entity.EntityState = EntityState.Old;
                }
                else
                {
                    UpdateDomainEntity(entity);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(GetType().ToString() + ".ProcessSaveEntity", ex);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            DomainEntity entity = new DomainEntity();

            // initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteDomainEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Method
        public DomainEntity GetDomainByOID(string DomainOID)
        {
            Hashtable htKeys = new Hashtable();
            htKeys["OID"] = DomainOID;

            return GetEntity(htKeys) as DomainEntity;
        }

        public IList<DomainEntity> GetRootDomain()
        {
            return SqlMap.QueryForList<DomainEntity>("SelectRootDomainEntity", null) as IList<DomainEntity>;
        }

        public IList<DomainEntity> GetAllDomains()
        {
            return SqlMap.QueryForList<DomainEntity>("SelectAllDomainEntity", null) as IList<DomainEntity>;
        }

        public IList<DomainEntity> GetChildDomain(string OID)
        {
            if (string.IsNullOrEmpty(OID))
            {
                return null;
            }
            return SqlMap.QueryForList<DomainEntity>("SelectChildDomainByOID", OID);
        }

        public IList<DomainEntity> GetDomainsByQuerytionOID(string questionOID)
        {
            return SqlMap.QueryForList<DomainEntity>("SelectDomainsByQuestionOID", questionOID);
        }

        public IList<DomainEntity> GetExpertGoodAtDomains(string expertOID)
        {
            return SqlMap.QueryForList<DomainEntity>("SelectExpertGoodAtDomains", expertOID);
        }

        public int GetDomainQuestionCount(string DomainOID)
        {
            return SqlMap.QueryForObject<int>("GetDomainQuestionCount", DomainOID);
        }

        #endregion
    }
}